﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class MenuList : System.Web.UI.Page
    {
        protected DataTable dt;
        //DataTable dtMenuList;
        Phoenix.AppCode.Entities.MenuList menulist = new Phoenix.AppCode.Entities.MenuList();
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'>" + "Danh mục được thêm mới thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'>" + "Thông tin danh mục chưa được cập nhật." + "</p></div><br  class='clear'/>";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    ddlMenuBack_FrontEnd.Items.Insert(0, new ListItem("Người dùng", "0"));
                    ddlMenuBack_FrontEnd.Items.Insert(1, new ListItem("Quản lý", "1"));

                    if (Request.Cookies["ddlMenuBack_FrontEnd"] == null)
                    {
                        Response.Cookies["ddlMenuBack_FrontEnd"].Value = "0";
                        Response.Cookies["ddlMenuBack_FrontEnd"].Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(Request.Cookies["ddlMenuBack_FrontEnd"]);
                    }
                    dt = Phoenix.AppCode.Bussiness.MenuListService.MenuListListByBeFe(Convert.ToInt32(Request.Cookies["ddlMenuBack_FrontEnd"].Value.Trim()), Request.Cookies["Lang_Backend"].Value);

                    ddlMenuBack_FrontEnd.SelectedValue = Request.Cookies["ddlMenuBack_FrontEnd"].Value;

                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "OnOff" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = true;
                        edit.Visible = true;
                        Load_ActionMenuList(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("ProfileInfo.aspx");
                }
            }
        }

        #region  Load action MenuList
        private void Load_ActionMenuList(string action)
        {
            //panelCreateCategory.Visible = visible;
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = false;

                    // load drop down list MenuList type
                    ddlMenuNameA.DataSource = dt;
                    ddlMenuNameA.DataTextField = "MenuName";
                    ddlMenuNameA.DataValueField = "ID_MenuList";
                    ddlMenuNameA.DataBind();
                    ddlMenuNameA.Items.Insert(0, new ListItem("None", "0"));

                    ddlMenuBack_FrontEndA.Items.Insert(0, new ListItem("Người dùng", "0"));
                    ddlMenuBack_FrontEndA.Items.Insert(1, new ListItem("Quản lý", "1"));

                    ddlMenuBack_FrontEndA.SelectedValue = Request.Cookies["ddlMenuBack_FrontEnd"].Value;
                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = false;

                    // load drop down list MenuList type
                    ddlMenuNameE.DataSource = dt;
                    ddlMenuNameE.DataTextField = "MenuName";
                    ddlMenuNameE.DataValueField = "ID_MenuList";
                    ddlMenuNameE.DataBind();
                    ddlMenuNameE.Items.Insert(0, new ListItem("None", "0"));

                    ddlMenuBack_FrontEndE.Items.Insert(0, new ListItem("Người dùng", "0"));
                    ddlMenuBack_FrontEndE.Items.Insert(1, new ListItem("Quản lý", "1"));

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.MenuListService.MenuListList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    ddlMenuBack_FrontEndE.SelectedValue = dt_Edit.Rows[0]["MenuBack_FrontEnd"].ToString();
                    txtMenuNameE_vi.Text = dt_Edit.Rows[0]["MenuName"].ToString();
                    txtMenuNameE_en.Text = dt_Edit.Rows[1]["MenuName"].ToString();
                    ddlMenuNameE.SelectedValue = dt_Edit.Rows[0]["ID_MenuList"].ToString();
                    txtURLE.Text = dt_Edit.Rows[0]["URL"].ToString();
                    txtLocationE.Text = dt_Edit.Rows[0]["Location"].ToString();

                    // load drop down list MenuList type
                    //dtMenuList = Phoenix.AppCode.Bussiness.MenuListService.MenuListListByBeFe(Convert.ToInt32(dt_Edit.Rows[0]["MenuBack_FrontEnd"].ToString()), Request.Cookies["Lang_Backend"].Value);
                }
                else if (action == "Delete")
                {
                    try
                    {
                        menulist = Phoenix.AppCode.Bussiness.MenuListService.GetMenuListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.MenuListService.Delete(menulist);
                        lblWarning.Text = success;
                        //Response.Redirect("MenuList.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("MenuList.aspx");
                    }
                }
                else if (action == "OnOff")
                {
                    try
                    {
                        menulist = Phoenix.AppCode.Bussiness.MenuListService.GetMenuListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.MenuListService.OnOff(menulist);
                        lblWarning.Text = success;
                        //Response.Redirect("MenuList.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("MenuList.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                menulist.MenuName_vi = txtMenuNameA_vi.Text;
                menulist.MenuName_en = txtMenuNameA_en.Text;
                menulist.ID_MenuList_Parent = Convert.ToInt32(ddlMenuNameA.SelectedValue);
                menulist.URL = txtURLA.Text;
                menulist.MenuBack_FrontEnd = Convert.ToInt32(ddlMenuBack_FrontEndA.SelectedValue);
                menulist.Location = Convert.ToInt32(txtLocationA.Text);

                Phoenix.AppCode.Bussiness.MenuListService.Create(menulist);

                ClearAllFields();
                lblWarningA.Text = success;
                
                //Response.Redirect("MenuList.aspx?Action=Create");
            }
            catch
            {
                lblWarningA.Text = error;
                //Response.Redirect("MenuList.aspx?Action=Create");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            lblWarningA.Text = info;
            ClearAllFields();
        }

        private void ClearAllFields()
        {
            txtMenuNameA_vi.Text = "";
            txtMenuNameA_en.Text = "";
            txtURLA.Text = "";
            txtLocationA.Text = "";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                menulist = Phoenix.AppCode.Bussiness.MenuListService.GetMenuListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                menulist.MenuName_vi = txtMenuNameE_vi.Text;
                menulist.MenuName_en = txtMenuNameE_en.Text;
                menulist.ID_MenuList_Parent = Convert.ToInt32(ddlMenuNameE.SelectedValue);
                menulist.URL = txtURLE.Text;
                menulist.MenuBack_FrontEnd = Convert.ToInt32(ddlMenuBack_FrontEndE.SelectedValue);
                menulist.Location = Convert.ToInt32(txtLocationE.Text);

                Phoenix.AppCode.Bussiness.MenuListService.Edit(menulist);
                lblWarningE.Text = success;
                //Response.Redirect("MenuList.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");
            }
            catch {
                lblWarningE.Text = error;
            }

        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            lblWarningE.Text = info;
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.MenuListService.MenuListList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            ddlMenuBack_FrontEndE.SelectedValue = dt_Edit.Rows[0]["MenuBack_FrontEnd"].ToString();
            txtMenuNameE_vi.Text = dt_Edit.Rows[0]["MenuName"].ToString();
            txtMenuNameE_en.Text = dt_Edit.Rows[1]["MenuName"].ToString();
            ddlMenuNameE.SelectedValue = dt_Edit.Rows[0]["ID_MenuList_Parent"].ToString();
            txtURLE.Text = dt_Edit.Rows[0]["URL"].ToString();
            txtLocationE.Text = dt_Edit.Rows[0]["Location"].ToString();
        }

        protected void ddlMenuBack_FrontEnd_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.MenuListService.MenuListListByBeFe(Convert.ToInt32(ddlMenuBack_FrontEnd.SelectedValue), Request.Cookies["Lang_Backend"].Value);
            Response.Cookies["ddlMenuBack_FrontEnd"].Value = ddlMenuBack_FrontEnd.SelectedValue;
        }

        protected void ddlMenuBack_FrontEndA_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.MenuListService.MenuListListByBeFe(Convert.ToInt32(ddlMenuBack_FrontEndA.SelectedValue), Request.Cookies["Lang_Backend"].Value);
            Response.Cookies["ddlMenuBack_FrontEnd"].Value = ddlMenuBack_FrontEndA.SelectedValue;
            
            // load drop down list MenuList type
            ddlMenuNameA.DataSource = dt;
            ddlMenuNameA.DataTextField = "MenuName";
            ddlMenuNameA.DataValueField = "ID_MenuList";
            ddlMenuNameA.DataBind();
            ddlMenuNameA.Items.Insert(0, new ListItem("None", "0"));
        }

        protected void ddlMenuBack_FrontEndE_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.MenuListService.MenuListListByBeFe(Convert.ToInt32(ddlMenuBack_FrontEndE.SelectedValue), Request.Cookies["Lang_Backend"].Value);
            Response.Cookies["ddlMenuBack_FrontEnd"].Value = ddlMenuBack_FrontEndE.SelectedValue;

            // load drop down list MenuList type
            ddlMenuNameE.DataSource = dt;
            ddlMenuNameE.DataTextField = "MenuName";
            ddlMenuNameE.DataValueField = "ID_MenuList";
            ddlMenuNameE.DataBind();
            ddlMenuNameE.Items.Insert(0, new ListItem("None", "0"));
        }
    }
}